<?php
declare (strict_types = 1);

namespace app\demo\controller;

use app\BaseController;
use QL\QueryList;
use GuzzleHttp\Client;
use think\facade\Queue;
use think\facade\Db;
use think\facade\Event;
/**
 * Class index
 * @package app\demo\controller
 */
class index  extends BaseController
{

    //要爬取的目标网址，用来获取IP,存入IP池
    protected $re_url = 'http://www.66ip.cn/areaindex_5/1.html';

    //要爬取的资源网站
    protected $crawling_url = '';

    
    /**
     * @return string
     */
    public function index()
    {
        return '您好！这是一个[demo]示例应用';
    }

    /**
     * 动态获取ip池，存入redis做爬虫使用
     */
    public function getIPPool()
    {


//        $rules = [
//            'ip'     => ['#footer>div>table>tr:eq(5)>td:eq(0)','html'],
////            '端口号'  => ['#footer>div>table>td>:eq(1)>td:eq(1)','html'],
////            '代理位置'=> ['#footer>div>table>td>:eq(1)>td:eq(2)','html'],
////            '代理类型'=> ['#footer>div>table>td>:eq(1)>td:eq(3)','html'],
////            '验证时间'=> ['#footer>div>table>td>:eq(1)>td:eq(4)','html']
//        ];
//
//        $ql->rules($rules)->encoding('UTF-8','GB2312')->removeHead()->query();
//
//        $data = $ql->getData();
//
//           halt($data);
//        $redis = new \Redis();
//
//         for($i=1;$i<=44;$i++)
//         {
//             $ql->rules($rules)->encoding('UTF-8','GB2312')->removeHead()->query();
//
//             $data = $ql->getData();
//
//         }


//        $html = file_get_contents($this->re_url);
//
//        $ql = QueryList::html($html);

        //爬取表格
//        $client = new Client();
//        $res = $client->request('GET', $this->re_url);
//
//        $html = (string)$res->getBody()->;
//         print_r($html);die;
        
        // 采集表头
//        $tableHeader = $table->find('tr:eq(0)')->find('td')->texts();
        set_time_limit(0);
        $url  ='http://www.66ip.cn/areaindex_';
//        $url  ='http://www.66ip.cn/areaindex_1/1.html';

        $jobHandlerClassName  = 'app\demo\job1';
        $jobQueueName  	  = "HelloJob";
        for ($i=1;$i<=34;$i++)
        {
         if($i>1){

             $url = "$url.$i/$i._html";
         }

            $html = file_get_contents($url);

            $table = QueryList::get($html)->find('table');


            
//            $table = QueryList::html($html)->find('table');
            unset($html);
//             halt($table);
            // 采集表的每行内容
            $tableRows = $table->find('tr:gt(1)')->map(function($row){
                return $row->find('td')->texts()->all();
            });
             
//             halt($tableRows);
            foreach ($tableRows as $key=> $value){
                $jobData =[
                    'IP'        => $value[0],
                    'port'      => $value[1],
                    'dai_tool'  => $value[2],
                    'type'      => $value[3],
                    'time'      => $value[4],
                ];
//                '2020年03月23日22时 验证'
                
                $isPushed = Queue::push($jobHandlerClassName,$jobData,$jobQueueName);
                if(!$isPushed){
                    halt($isPushed);
                }
            }
        }
    }


    public function insertIp()
    {
//        \think\facade\Db::name('ip_tool')->insert([
//            'IP'           =>'asdasda',
//            'port'         =>'9999',
//            'dai_tool'     => 'adasda',
//            'type'         =>'asdsadasdas',
//            'verification_time'=>'asdas',
//        ]);

    }


    /**
     * redis -练手
     */
    public function redisTest()
    {
        //
    }

    /**
     * 
     */
    public function  requestForComments()
    {

        // 触发UserLogin事件 用于执行用户登录后的一系列操作
//      halt(Event::trigger('UserLogin',$this->request->ip()));
//
//      print_r('请求的所有变量:'.implode(',',$this->request->request()).PHP_EOL);
//      print_r('获取URL访问根地址:'.$this->request->domain().PHP_EOL);
//      print_r('获取当前根域名:'.$this->request->rootDomain().PHP_EOL);
//      print_r('获取当前完整URL 包括QUERY_STRING:'.$this->request->url().PHP_EOL);
//      print_r('获取当前URL 不含QUERY_STRING:'.$this->request->baseUrl().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取URL访问根目录:'.$this->request->rootUrl().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);
//      print_r('获取当前执行的文件 SCRIPT_NAME:'.$this->request->baseFile().PHP_EOL);

//        https://gitee.com/ruoshuiyx/tp6.git
//        $arr = array(3,5,8,4,9,6,1,7,2);

//        halt(implode(' ',$arr));

//        php think make:command Hello hello
//
//        php think make:command app\demo\Command Test
//        $num1 = 123;
//        $num2 = 456;
//        $txt = vsprintf("%x%X",array($num1,$num2));
//        echo $txt;

        $a = '[{"cate_id":1,"cate_name":"\u5e2e\u52a9\u4e2d\u5fc3","article_id":3,"title":"\u8425\u9500\u6d3b\u52a8\u8bf4\u660e"},{"cate_id":1,"cate_name":"\u5e2e\u52a9\u4e2d\u5fc3","article_id":2,"title":"\u4ed8\u6b3e\u7ed3\u7b97\u8bf4\u660e"},{"cate_id":1,"cate_name":"\u5e2e\u52a9\u4e2d\u5fc3","article_id":4,"title":"\u4ef7\u683c\u8bf4\u660e"},{"cate_id":2,"cate_name":"\u5e73\u53f0\u4ecb\u7ecd","article_id":9,"title":"\u5546\u5bb6\u5956\u60e9\u89c4\u5219"},{"cate_id":2,"cate_name":"\u5e73\u53f0\u4ecb\u7ecd","article_id":8,"title":"\u89c4\u5219\u53d8\u66f4\u516c\u793a"},{"cate_id":2,"cate_name":"\u5e73\u53f0\u4ecb\u7ecd","article_id":7,"title":"\u653f\u5e9c\u53ca\u534f\u8bae"},{"cate_id":2,"cate_name":"\u5e73\u53f0\u4ecb\u7ecd","article_id":6,"title":"\u5e73\u53f0\u8d44\u8d28"},{"cate_id":2,"cate_name":"\u5e73\u53f0\u4ecb\u7ecd","article_id":5,"title":"\u516c\u53f8\u7b80\u4ecb"}]
';

//        foreach ($this->json as $key =>$item)
//        {
//
//        }

        $aaa =  json_decode($a,true);
        halt($aaa);
    }



}


